package com.zhongjian.technologycal.mall.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @设置时间倒计时
 * 
 */
public class Countdown {
	private TextView timeTv;
	private long levaeTime;
	private String agr0;
	private boolean isDay;
    private Context context;
	// 设置时间倒计时
	public Countdown(TextView timeTv, long levaeTime, String agr0, Boolean isDay) {
		this.agr0 = agr0;
		this.timeTv = timeTv;
		this.levaeTime = levaeTime;
		this.isDay = isDay;
		setLavetime(levaeTime);
		if (!isTimerRun) {
			runTime();
		}
	}

	Timer timer = null;
	private boolean isTimerRun = false;

	private void runTime() {
		timer = new Timer();
		isTimerRun = true;
		timer.scheduleAtFixedRate(new TimerTask() {

			@Override
			public void run() {
				if (levaeTime > 0) {
					levaeTime--;
					iniTime.sendEmptyMessage(0);
				} else {

				}

			}
		}, 1000, 1000);
	}

	Handler iniTime = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			setLavetime(levaeTime);
		}
	};

	private void setLavetime(long mss) {
		long days = mss / (60 * 60 * 24);
		long hours = (mss % (60 * 60 * 24)) / (60 * 60);
		long minutes = (mss % (60 * 60)) / (60);
		long seconds = (mss % (60));

		if (isDay) {
			timeTv.setText(agr0 + days + "天" + hours + "小时" + minutes + "分"
					+ seconds + "秒");
		} else {

			if (mss == 0) {
				timeTv.setText("重新获取");
				timeTv.setTextColor(0xff3A9FFF);
				timeTv.setClickable(true);
			} else {
				timeTv.setTextColor(0xffA4A4A4);
				timeTv.setText(mss + agr0);
			}
		}
	}

}
